﻿using CommonEntity.CommonImageEntities;
using ReactiveUI.Fody.Helpers;
using ReactiveUI;
using ScienceAppService.ModuleInterface;
using System.Reactive;
using System.Reactive.Linq;

namespace ScienceAIApp.ViewModels.SubViewModels;
public class ImageRecordViewModel : ScienceBaseViewModel
{

    private IHistoryImageService _historyImageService;

    [Reactive] public ImageRecord? CurrentRecord { get; private set; }

    [Reactive] public string ShowDescription { get; set; }
    [Reactive] public string Dimension { get; set; }
    [Reactive] public string CreateTime { get; set; }
    [Reactive] public bool IsChecked { get; set; }


    public ReactiveCommand<Unit, Unit> ChooseRecordCommand;

    public ReactiveCommand<Unit, Unit> DeleteRecordCommand;

    // 这里的是目录
    public Interaction<ImageRecord, Unit> ChooseRecordInteraction;

    public Interaction<ImageRecord, Unit> DeleteRecordInteraction;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="imageShowService"></param>
    public ImageRecordViewModel(IHistoryImageService historyImageService)
    {
        _historyImageService = historyImageService;

        ShowDescription = "";
        Dimension = "";
        CreateTime = "";

        ChooseRecordInteraction = new Interaction<ImageRecord, Unit>();
        DeleteRecordInteraction = new Interaction<ImageRecord, Unit>();

        // 选中 record操作
        ChooseRecordCommand = ReactiveCommand.CreateFromTask(async () =>
        {
            IsChecked = true;
            ChooseRecordInteraction?.Handle(CurrentRecord!);
        });

        // 删除 record操作
        DeleteRecordCommand = ReactiveCommand.CreateFromTask(async () =>
        {
            await _historyImageService.DeleteRecord(CurrentRecord!.Id);
            await DeleteRecordInteraction?.Handle(CurrentRecord);
        });

        _historyImageService = historyImageService;
    }

    /// <summary>
    /// 设置序列
    /// </summary>
    /// <param name="si"></param>
    public void SetRecord(ImageRecord? record)
    {
        if (record == null)
        {
            CurrentRecord = null;
            ShowDescription = "";
            Dimension = "";
            CreateTime = "";
            IsChecked = false;
        }
        else
        {
            CurrentRecord = record;
            ShowDescription = record.SeriesDescription;
            Dimension = record.Dimension;
            CreateTime = record.CreateTime.ToString();
            IsChecked = false;
        }
    }
}
